<?php
require_once("includes/pagetop.php");
if($_FILES["selectedFile"]!="")
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		$target_path = 'uploads/blog_images/';
			
		//Remove previously uploaded files according to time
	//	$expiration=7200; 
	//	$img_path=$target_path;
		//list($usec, $sec) = explode(" ", microtime());
	//	$now = ((float)$usec + (float)$sec);
	//
	//	$current_dir = @opendir($img_path);
	//
	//	while ($filename = @readdir($current_dir))
	//	{
	//		if ($filename != "." and $filename != ".." and $filename != "index.html")
	//		{
	//			$name = str_replace(array(".jpg",".jpeg",".png",".bmp",".gif"), "", $filename);
	//
	//			if (($name + $expiration) < $now)
	//			{
	//				@unlink($img_path.$filename);
	//			}
	//		}
	//	}
	//
	//	@closedir($current_dir);
		// Deleting Old files code ends here
		
		list($usec, $sec) = explode(" ", microtime());
		$now = ((string)((float)$usec + (float)$sec));
		$ext = explode('.', basename( $_FILES['selectedFile']['name']));
		
		if(!in_array($ext,array(".jpg",".jpeg",".png",".bmp",".gif")))
		{
			$fileCounter++;
			$filePath=$now . "." . $ext[count($ext)-1];
			$Fullpath = $target_path .$filePath; 
		
			if(move_uploaded_file($_FILES['selectedFile']['tmp_name'], $Fullpath)) 
			{
				$condition=array(":user_id"=>$_SESSION['Allyfolio_User_ID']);
				$qryBlogPageInfo="select * from af_blog_page where user_id =:user_id ";
    			$arrBlogPageInfo=$pdodbobj->fetch_array($pdodbobj->query($qryBlogPageInfo,$condition));
    			
    			$condition=array(":blog_page_image"=>$filePath,":user_id"=>$_SESSION['Allyfolio_User_ID']);
    			if(count($arrBlogPageInfo)>0)
    			{
					$qryUpdate="update af_blog_page set blog_page_image=:blog_page_image where user_id =:user_id ";
					$pdodbobj->query($qryUpdate,$condition);
				}
				else
				{
					$qryInsert="insert into af_blog_page set blog_page_image=:blog_page_image,  user_id =:user_id ";
					$pdodbobj->query($qryInsert,$condition);
				}
				
				echo "SUCCESS|~|".$filePath;
			} 
			else
			{
				echo "ERROR|~|Looks like there is some problem. Please try after some time.";
			}
		}
		else
		{
			echo "ERROR|~|Please Upload image only.";
		}
	}
	else
	{
		echo "ERROR|~|Please login to upload image.";
	}
}

else if($_REQUEST["purpose"]=="showlinks" && $_REQUEST["blog_id"]>0)
{
	$qryLinks="select * from  af_blog_links where blog_id=:blog_id";
	$arrBlogLinks=$pdodbobj->fetch_array($pdodbobj->query($qryLinks,array("blog_id"=>$_REQUEST["blog_id"])));
?>
    <table style="width: 100%;height: 100%;">
        <tr>
            <td align="center" style="vertical-align: middle;">
                <div style="background-color: white;border:2px solid black;width:700px;">
					<table cellpadding="10px" cellspacing="8px" width="700px" style="height: 350px;" >
			<?php
            	if(count($arrBlogLinks)>0)
            	{
					for($i=0; $i<count($arrBlogLinks);$i++)
					{
				?>	
                        <tr>
                            <td style="font-size: 23px;color: #0b1419;font-weight: bold;text-shadow: 1px 2px 3px #999;">#<?php echo ($i+1); ?>:</td>
                            <td ><a href="<?php echo $arrBlogLinks[$i]["blog_link"]; ?>" target="_blank"><?php echo $arrBlogLinks[$i]["blog_link"]; ?></a></td>
                        </tr>
                <?php
                 	}
				?>       
			<?php		
				}
				else
				{
			?>
				<tr>
					<td colspan="2"><div align="center" style="width:100%"><span class="error_span">Sorry, there is no external links for this blog.</span></div></td>
				</tr>
			<?php		
				}
			?>    		
						<tr>
        					<td>&nbsp;</td>
        					<td><input type="button" onclick="document.getElementById('links_div').style.display='none';" class="up_btn" value="Close" /></td>
        				</tr>
             		 </table>
            	</div>
            </td>
        </tr>
    </table>
<?php	
}
?>